The present invention relates generally to a method and apparatus of formatting values; and more particularly, to such a method and apparatus using the Java programming language. Still more particularly, the present invention relates to a method and apparatus to format values in a fixed number of spaces having leading or trailing characters using the Java programming language.
Java is a programming language designed to generate applications capable of executing on all hardware platforms without modification. Java was originally developed in 1991 by Sun Microsystems, Inc. as a language for embedded applications. Java is an interpreted language wherein the source code is compiled into an intermediate language called bytecode. The bytecode is converted or interpreted into machine code at runtime. Thus, Java programs are not dependent on any specific hardware and run in any computer having a Java virtual machine. The Java programming language includes similar functionality of other programming languages, e.g., C or C++; however, Java does not provide a method to print out formatted lines, e.g., if a programmer wants to print out a table of numbers with several columns with each column of numbers lining up. Currently, this only works if each number uses the same number of columns. If the numbers use differing numbers of columns, the columns of numbers will not line up. For example, the number xe2x80x9c3xe2x80x9d uses a single column; however, the number xe2x80x9c1234xe2x80x9d requires four columns. This leads to inconsistent and unpleasant formatting of output, as in Column A of Listing 1 below.
Column B is a formatted output of numeric values wherein the numeric values occupy the same number of digits or spaces, i.e., five spaces in this example. It is important to note that the values in column B which are less than five digits long are padded with zeroes to make them equal lengths.
It is known in the art to use a format string in the C programming language to obtain the above-described format and functionality. For example, to print out a number using five spaces or columns with leading zeroes using the C programming language, a programmer would use the formatting string, xe2x80x9c%05dxe2x80x9d, resulting in the output as shown in Column B of Listing 1. Unfortunately, the present inventor is unaware of any method in the Java programming language for obtaining such functionality using already existing formatting statements. In the context of this document, method and function are used interchangeably.
Instead, to obtain such functionality other approaches have been used including using a series of nested if statements or a looping mechanism; however, these methods are more time consuming, i.e., more processing time is required. The other approaches require a variable amount of time depending on the implementation.
A pseudo code-based example of using nested if statements is shown in Listing 2 below.
If (number=0)
Print 6 zeroes
Else if (number less than 10)
Print 5 zeroes
Else if (number less than 100)
Print 4 zeroes
. . .
Listing 2
With respect to Listing 2, xe2x80x9cnumberxe2x80x9d is a variable storing the numerical value of the number to be formatted. Listing 2 operates to format the printed version of a numeric value to use a total of six digits or columns, e.g., a value of 55 is to be formatted as 000055 and a value of 5 as 000005. Each pseudo code if statement tests the size of the value of the number in order to format the printed output. For example, if the number is less than the value ten, then only five zeroes need to precede the number in an output to format it as a six digit value because the number is only a single digit, or column, wide. As shown in Listing 2, for small numbers, i.e., numbers having few digits, only a small portion of the if statements are executed. However, for larger numbers, it is possible that each of the if statements may need to be executed to properly format the output. This is problematic as it requires a variable amount of processing time.
Use of a looping mechanism is similarly flawed requiring an unknown amount of processing to format a number. In using a loop structure, the number of zeroes to precede the number is determined by subtracting the size in characters of the number to be printed from the number of columns or digits desired in the formatted output. A loop would then be executed a number of iterations equal to the number of zeroes determined and during each pass through the loop the character xe2x80x9c0xe2x80x9d is added to a target string. Once the loop completes execution, the original number, as a character, is appended to the target string, as well.
A pseudo code-based example of using a loop is shown in Listing 3 below.
Num_of_zeroes=num_of_cols_to_usexe2x88x92size_(in_chars)_of_num_to_print
Loop for Num_of_zeroes iterations copying a xe2x80x9c0xe2x80x9d char to a target_string
Append num_to_print to target_string . . .
Listing 3
Num_of_zeroes is a variable containing a numeric value representing the number of zeroes to be output before the number to be printed. Num_of_cols_to_use is a variable containing a numeric value representing the number of output columns to be used by the formatted number. Size_(in_chars)_of_num_to_print is the character-based size of the numeric value to be printed or output. Target string is a string storing the formatted output number. As with the nested if statements approach described above, a variable amount of processing time is required to execute the loop-based formatting approach. Therefore, there is a need in the art for a method of printing formatted lines using the Java language requiring a predetermined amount of processing time.
Another approach has been to use or xe2x80x9ccalloutxe2x80x9d a separate function or library programmed in a different language, e.g., the C programming language, to execute the formatting statement, i.e., the C format xe2x80x9c%05dxe2x80x9d statement described above. However, because there is a large amount of overhead or additional processing required to setup and perform such a callout in the Java language, there is a large performance and resource penalty. It is known to be very inefficient to callout to a separate function or library from within Java in comparison to performing the same function in Java. Thus, there is a need in the art for a Java-based formatting function requiring minimal additional processing overhead.
When resources, e.g., processor time and memory, are constrained, it is important to minimize the resource requirements. For example, in a web site or web server environment, the resources used to format printing reduces the resources available for serving customers or traffic to a web site. This means it is important to reduce the resource usage of the formatting function.
It is therefore an object of the present invention to provide a method of printing formatted lines using the Java language requiring a predetermined amount of processing time.
Another object of the present invention is to provide a Java-based formatting function requiring minimal additional processing overhead.
The above described objects are fulfilled by a computer implemented method of and apparatus for printing values in a fixed number of column spaces using Java. A value desired to be formatted to a predetermined length string and an indexed lookup table having predetermined formatted string entries are used. An index based on the number of characters needed to represent the value is calculated and then used to locate a desired length formatted string entry in the lookup table. The desired length formatted string entry is then combined with a string representation of the value to form a predetermined length string.
A computer implemented Java-based method aspect of formatting a value to a predetermined length string uses a lookup table. An index is calculated based on the number of characters required to represent the value. A desired length formatted string is obtained from the lookup table using the index. The desired length formatted string is combined with a string representation of the value to form a predetermined length string. In a further embodiment, the index is calculated based on the predetermining length of the predetermined length string and the maximum length of the lookup table entries. Uniquely, the method requires a constant determinable amount of processing time to perform the formatting.
A computer system for formatting a value to a predetermined length string using a lookup table includes a processor for receiving and transmitting data and a memory coupled to the processor. The memory has stored sequences of Java-based instructions which, when executed by the processor, cause the processor to calculate an index based on the number of characters required to represent the value. Further, the instructions cause the processor to locate a desired length formatted string in the lookup table using the calculated index and combine the desired length formatted string with a string representation of the value, thus forming a predetermined length formatted string. Advantageously, because the instructions are Java-based there is minimal additional processing required to callout to a separate non-Java-based function.